package gnv;

import net.sf.paperclips.DefaultGridLook;
import net.sf.paperclips.GridColumn;
import net.sf.paperclips.GridPrint;
import net.sf.paperclips.ImagePrint;
import net.sf.paperclips.LineBorder;
import net.sf.paperclips.Print;
import net.sf.paperclips.TextPrint;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class PrintHandler {

	  Table table;
	
	
	  public PrintHandler() {
		// TODO Auto-generated constructor stub
	}
	  
	  
	  public Print createSelectedPrint (Table table){
		    // Create GridPrint with all columns at default size.

		    DefaultGridLook look = new DefaultGridLook();
		    
		    
		    
		    look.setCellBorder( new LineBorder() );
		    RGB background = table.getDisplay().getSystemColor( SWT.COLOR_WIDGET_BACKGROUND ).getRGB();
		    look.setHeaderBackground( background );
//		    look.setFooterBackground( background );

		    GridPrint grid = new GridPrint( look );

		    // Add header and footer to match table column names.
		    TableColumn[] columns = table.getColumns();
		    for ( int i = 0; i < columns.length; i++ ) {
		      TableColumn col = columns[i];

		      // Add the column to the grid with alignment applied, default width, no
		      // weight
		      grid.addColumn( new GridColumn( col.getAlignment(), SWT.DEFAULT, 0 ) );

		      Print cell = createCell( col.getImage(), col.getText(), SWT.CENTER );
		      grid.addHeader( cell );
		    }
		    // Add content rows
		    TableItem[] items = table.getItems();
		    for ( int i = 0; i < table.getItemCount(); i++ ) {
		    	  	TableItem item = items[i];
		    	  	boolean check = item.getChecked();
		    	  	if (check) {
		  		      for ( int j = 0; j < columns.length; j++ )
		  		        grid.add( createCell( item.getImage( j ), item.getText( j ), columns[j].getAlignment() ) );
				}
		    }

		    return grid;
	  }
	  
	  public Print createPrint( Table table ) {
		    // Create GridPrint with all columns at default size.

		    DefaultGridLook look = new DefaultGridLook();
		    
		    
		    
		    look.setCellBorder( new LineBorder() );
		    RGB background = table.getDisplay().getSystemColor( SWT.COLOR_WIDGET_BACKGROUND ).getRGB();
		    look.setHeaderBackground( background );

		    GridPrint grid = new GridPrint( look );

		    // Add header and footer to match table column names.
		    TableColumn[] columns = table.getColumns();
		    for ( int i = 0; i < columns.length; i++ ) {
		      TableColumn col = columns[i];

		      // Add the column to the grid with alignment applied, default width, no
		      // weight
		      grid.addColumn( new GridColumn( col.getAlignment(), SWT.DEFAULT, 0 ) );

		      Print cell = createCell( col.getImage(), col.getText(), SWT.CENTER );
		      grid.addHeader( cell );
		  //    grid.addFooter( cell );
		    }

		    // Add content rows
		    TableItem[] items = table.getItems();
		    for ( int i = 0; i < items.length; i++ ) {
		      TableItem item = items[i];
		      for ( int j = 0; j < columns.length; j++ )
		        grid.add( createCell( item.getImage( j ), item.getText( j ), columns[j].getAlignment() ) );
		    }

		    return grid;
		  }

		  public static Print createCell( Image image, String text, int align ) {
		    if ( image == null )
		      return new TextPrint( text, align );

		    GridPrint grid = new GridPrint( "p, d" );
		    grid.add( new ImagePrint( image.getImageData(), image.getDevice().getDPI() ) );
		    grid.add( new TextPrint( text, align ) );
		    
		    return grid;
		  }

	
	
}
